<?php
/**
 * 商品分类功能测试文件
 */

require_once __DIR__ . '/backend/support/bootstrap.php';

use app\model\GoodsCatModel;
use app\model\GoodsModel;

/**
 * 测试商品分类数据
 */
function testGoodsCategories() {
    echo "=== 测试商品分类功能 ===\n";
    
    try {
        // 检查分类表是否存在数据
        $categories = GoodsCatModel::where('status', 1)->get();
        echo "分类总数: " . $categories->count() . "\n";
        
        if ($categories->count() == 0) {
            echo "没有找到分类数据，创建测试分类...\n";
            createTestCategories();
        }
        
        // 显示分类结构
        displayCategoryTree();
        
    } catch (Exception $e) {
        echo "测试失败: " . $e->getMessage() . "\n";
    }
    
    echo "\n";
}

/**
 * 创建测试分类数据
 */
function createTestCategories() {
    try {
        // 检查是否已有测试数据
        $existingCategories = GoodsCatModel::where('name', 'like', '%测试分类%')->count();
        
        if ($existingCategories > 0) {
            echo "已存在测试分类数据\n";
            return;
        }
        
        // 创建主分类
        $mainCategories = [
            ['name' => '数码产品', 'fid' => 0, 'sort' => 1, 'status' => 1],
            ['name' => '服装鞋帽', 'fid' => 0, 'sort' => 2, 'status' => 1],
            ['name' => '家居用品', 'fid' => 0, 'sort' => 3, 'status' => 1],
            ['name' => '食品饮料', 'fid' => 0, 'sort' => 4, 'status' => 1]
        ];
        
        $mainCatIds = [];
        foreach ($mainCategories as $catData) {
            $category = new GoodsCatModel();
            $category->fill($catData);
            $category->save();
            $mainCatIds[] = $category->id;
            echo "创建主分类: {$catData['name']} (ID: {$category->id})\n";
        }
        
        // 创建子分类
        $subCategories = [
            // 数码产品子分类
            ['name' => '手机', 'fid' => $mainCatIds[0], 'sort' => 1, 'status' => 1],
            ['name' => '电脑', 'fid' => $mainCatIds[0], 'sort' => 2, 'status' => 1],
            ['name' => '相机', 'fid' => $mainCatIds[0], 'sort' => 3, 'status' => 1],
            
            // 服装鞋帽子分类
            ['name' => '男装', 'fid' => $mainCatIds[1], 'sort' => 1, 'status' => 1],
            ['name' => '女装', 'fid' => $mainCatIds[1], 'sort' => 2, 'status' => 1],
            ['name' => '鞋靴', 'fid' => $mainCatIds[1], 'sort' => 3, 'status' => 1],
            
            // 家居用品子分类
            ['name' => '厨房用品', 'fid' => $mainCatIds[2], 'sort' => 1, 'status' => 1],
            ['name' => '清洁用品', 'fid' => $mainCatIds[2], 'sort' => 2, 'status' => 1],
            ['name' => '收纳用品', 'fid' => $mainCatIds[2], 'sort' => 3, 'status' => 1],
            
            // 食品饮料子分类
            ['name' => '零食', 'fid' => $mainCatIds[3], 'sort' => 1, 'status' => 1],
            ['name' => '饮料', 'fid' => $mainCatIds[3], 'sort' => 2, 'status' => 1],
            ['name' => '生鲜', 'fid' => $mainCatIds[3], 'sort' => 3, 'status' => 1]
        ];
        
        foreach ($subCategories as $catData) {
            $category = new GoodsCatModel();
            $category->fill($catData);
            $category->save();
            echo "创建子分类: {$catData['name']} (ID: {$category->id})\n";
        }
        
        echo "测试分类数据创建完成\n";
        
    } catch (Exception $e) {
        echo "创建测试分类失败: " . $e->getMessage() . "\n";
    }
}

/**
 * 显示分类树结构
 */
function displayCategoryTree() {
    try {
        $categories = GoodsCatModel::where('status', 1)
                                 ->orderBy('sort', 'asc')
                                 ->orderBy('id', 'asc')
                                 ->get();
        
        $catMap = [];
        
        // 构建分类树结构
        foreach ($categories as $category) {
            $catId = $category->id;
            $fid = $category->fid;
            
            if ($fid == 0) {
                // 主分类
                $catMap[$catId] = [
                    'id' => $catId,
                    'text' => $category->name,
                    'children' => []
                ];
            } else {
                // 子分类
                if (isset($catMap[$fid])) {
                    $catMap[$fid]['children'][] = [
                        'id' => $catId,
                        'text' => $category->name
                    ];
                }
            }
        }
        
        echo "\n分类树结构:\n";
        foreach ($catMap as $mainCat) {
            echo "├─ {$mainCat['text']} (ID: {$mainCat['id']})\n";
            foreach ($mainCat['children'] as $subCat) {
                echo "│  └─ {$subCat['text']} (ID: {$subCat['id']})\n";
            }
        }
        
    } catch (Exception $e) {
        echo "显示分类树失败: " . $e->getMessage() . "\n";
    }
}

/**
 * 测试API返回的分类数据格式
 */
function testCategoryApiFormat() {
    echo "\n=== 测试API分类数据格式 ===\n";
    
    try {
        $categories = GoodsCatModel::where('status', 1)
                                 ->orderBy('sort', 'asc')
                                 ->orderBy('id', 'asc')
                                 ->get();
        
        $categoryTree = [];
        $catMap = [];
        
        // 构建分类树结构
        foreach ($categories as $category) {
            $catId = $category->id;
            $fid = $category->fid;
            
            if ($fid == 0) {
                // 主分类
                $catMap[$catId] = [
                    'id' => $catId,
                    'text' => $category->name,
                    'children' => []
                ];
            } else {
                // 子分类
                if (isset($catMap[$fid])) {
                    $catMap[$fid]['children'][] = [
                        'id' => $catId,
                        'text' => $category->name
                    ];
                }
            }
        }
        
        $categoryTree = array_values($catMap);
        
        echo "API返回格式:\n";
        echo json_encode($categoryTree, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        echo "\n";
        
    } catch (Exception $e) {
        echo "测试API格式失败: " . $e->getMessage() . "\n";
    }
}

// 执行测试
echo "开始商品分类功能测试\n";
echo "==================\n\n";

testGoodsCategories();
testCategoryApiFormat();

echo "测试完成！\n";
echo "==================\n";
echo "分类功能说明：\n";
echo "1. 支持主分类和子分类两级结构\n";
echo "2. 分类数据来自goods_cat表\n";
echo "3. 支持分类排序和状态控制\n";
echo "4. API返回van-tree-select所需的数据格式\n"; 